//
//  CNDouble.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/3/13.
//

import Foundation
import CoreGraphics

extension Double {

    public func cn_stringFormat(decimals: Int) -> String {
        let formatter = NumberFormatter()
        formatter.minimumFractionDigits = decimals
        formatter.maximumFractionDigits = decimals
        return formatter.string(from: NSNumber(value: self)) ?? ""
    }

    public func cn_roundedTo(places: Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}

extension Double {

    public var cn_intValue: Int {
        return Int(self)
    }

    public var cn_stringValue: String {
        return "\(self)"
    }

    public var cn_floatValue: Float {
        return Float(self)
    }

    public var cn_cgfloatValue: CGFloat {
        return CGFloat(self)
    }

    public var cn_dataValue: Data {
        var value = self
        return Data(bytes: &value, count: MemoryLayout<Double>.size)
    }
}
